package com.jeesite.modules.dp.web.kecheng;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.dp.entity.dptype.DpType;
import com.jeesite.modules.dp.entity.kecheng.DpKecheng;
import com.jeesite.modules.dp.service.dptype.DpTypeService;
import com.jeesite.modules.dp.service.kecheng.DpKechengService;

/**
 * 课程管理Controller
 * @author hancing
 * @version 2024-12-02
 */
@Controller
@RequestMapping(value = "${adminPath}/dp/kecheng/dpKecheng")
public class DpKechengController extends BaseController {

	@Autowired
	private DpKechengService dpKechengService;
	@Autowired
	private DpTypeService dpTypeService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DpKecheng get(String id, boolean isNewRecord) {
		return dpKechengService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("dp:kecheng:dpKecheng:view")
	@RequestMapping(value = {"list", ""})
	public String list(DpKecheng dpKecheng, Model model) {
		model.addAttribute("dpKecheng", dpKecheng);
		return "modules/dp/kecheng/dpKechengList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("dp:kecheng:dpKecheng:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public List<DpKecheng> listData(DpKecheng dpKecheng, HttpServletRequest request, HttpServletResponse response) {
		return dpKechengService.findList(dpKecheng);
	}

	/**S
	 * 查看编辑表单
	 */
	@RequiresPermissions("dp:kecheng:dpKecheng:view")
	@RequestMapping(value = "form")
	public String form(DpKecheng dpKecheng, Model model) {
		model.addAttribute("dpKecheng", dpKecheng);
		return "modules/dp/kecheng/dpKechengForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("dp:kecheng:dpKecheng:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DpKecheng dpKecheng) {
		//设置typename为路径
		DpType dpType= dpTypeService.get(dpKecheng.getKctype());
		dpKecheng.setTypename(dpType.getTreeNames());
		dpKechengService.save(dpKecheng);
		return renderResult(Global.TRUE, text("保存课程成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("dp:kecheng:dpKecheng:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DpKecheng dpKecheng) {
		dpKechengService.delete(dpKecheng);
		return renderResult(Global.TRUE, text("删除课程成功！"));
	}
	
}